package com.sky.controller.user;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * ClassName: DishController
 * Package: com.sky.controller.user
 * Description:
 *
 * @Author 那啥啥
 * @Create 2024/3/17  20:54
 * @Version 1.0
 */
@RestController("userDishController")
@RequestMapping("/user/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     *
     * 根据分类id查询菜品信息
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品信息")
    public Result<List<DishVO>> list(Long categoryId){
        log.info("根据分类id查询菜品信息 categoryid{}",categoryId);
        //构造redis的key，规则dish_分类id
        String key  = "dish_"+categoryId;
//        查询redis中是否存在菜品数据
      List<DishVO> listR = (List<DishVO>) redisTemplate.opsForValue().get(key);
//        存在直接返回
        if(listR != null && listR.size()>0){
            return Result.success(listR);
        }
//        不存在查询数据库将查询到的数据放入redis

        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);
         listR = dishService.listWithFlavor(dish);

         redisTemplate.opsForValue().set(key,listR);
        return Result.success(listR);

    }
}
